{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "c2a8fc1d",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np # 数据处理最重要的模块\n",
    "import pandas as pd # 数据处理最重要的模块\n",
    "\n",
    "from IPython.core.interactiveshell import InteractiveShell # jupyter运行输出的模块\n",
    "\n",
    "#显示每一个运行结果\n",
    "InteractiveShell.ast_node_interactivity = 'all'\n",
    "\n",
    "#设置行不限制数量\n",
    "#pd.set_option('display.max_rows',None)\n",
    "\n",
    "#设置列不限制数量\n",
    "pd.set_option('display.max_columns', None)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "b5b50308",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CPU times: total: 93.8 ms\n",
      "Wall time: 93.3 ms\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Preclose</th>\n",
       "      <th>Open</th>\n",
       "      <th>Highest</th>\n",
       "      <th>Lowest</th>\n",
       "      <th>Close</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Day</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1990-12-19</th>\n",
       "      <td></td>\n",
       "      <td>96.050</td>\n",
       "      <td>99.980</td>\n",
       "      <td>95.790</td>\n",
       "      <td>99.980</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1990-12-20</th>\n",
       "      <td>99.98</td>\n",
       "      <td>104.300</td>\n",
       "      <td>104.390</td>\n",
       "      <td>99.980</td>\n",
       "      <td>104.390</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1990-12-21</th>\n",
       "      <td>104.39</td>\n",
       "      <td>109.070</td>\n",
       "      <td>109.130</td>\n",
       "      <td>103.730</td>\n",
       "      <td>109.130</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1990-12-24</th>\n",
       "      <td>109.13</td>\n",
       "      <td>113.570</td>\n",
       "      <td>114.550</td>\n",
       "      <td>109.130</td>\n",
       "      <td>114.550</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1990-12-25</th>\n",
       "      <td>114.55</td>\n",
       "      <td>120.090</td>\n",
       "      <td>120.250</td>\n",
       "      <td>114.550</td>\n",
       "      <td>120.250</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-08-25</th>\n",
       "      <td>3825.759</td>\n",
       "      <td>3848.163</td>\n",
       "      <td>3883.562</td>\n",
       "      <td>3839.972</td>\n",
       "      <td>3883.562</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-08-26</th>\n",
       "      <td>3883.562</td>\n",
       "      <td>3871.471</td>\n",
       "      <td>3888.599</td>\n",
       "      <td>3859.758</td>\n",
       "      <td>3868.382</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-08-27</th>\n",
       "      <td>3868.382</td>\n",
       "      <td>3869.612</td>\n",
       "      <td>3887.198</td>\n",
       "      <td>3800.350</td>\n",
       "      <td>3800.350</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-08-28</th>\n",
       "      <td>3800.35</td>\n",
       "      <td>3796.711</td>\n",
       "      <td>3845.087</td>\n",
       "      <td>3761.422</td>\n",
       "      <td>3843.597</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-08-29</th>\n",
       "      <td>3843.597</td>\n",
       "      <td>3842.823</td>\n",
       "      <td>3867.606</td>\n",
       "      <td>3839.206</td>\n",
       "      <td>3857.927</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>8473 rows × 5 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "            Preclose      Open   Highest    Lowest     Close\n",
       "Day                                                         \n",
       "1990-12-19              96.050    99.980    95.790    99.980\n",
       "1990-12-20     99.98   104.300   104.390    99.980   104.390\n",
       "1990-12-21    104.39   109.070   109.130   103.730   109.130\n",
       "1990-12-24    109.13   113.570   114.550   109.130   114.550\n",
       "1990-12-25    114.55   120.090   120.250   114.550   120.250\n",
       "...              ...       ...       ...       ...       ...\n",
       "2025-08-25  3825.759  3848.163  3883.562  3839.972  3883.562\n",
       "2025-08-26  3883.562  3871.471  3888.599  3859.758  3868.382\n",
       "2025-08-27  3868.382  3869.612  3887.198  3800.350  3800.350\n",
       "2025-08-28   3800.35  3796.711  3845.087  3761.422  3843.597\n",
       "2025-08-29  3843.597  3842.823  3867.606  3839.206  3857.927\n",
       "\n",
       "[8473 rows x 5 columns]"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%%time\n",
    "das= pd.read_csv(r\"D:\\python homework\\000001.csv\")\n",
    "das['Day'] = pd.to_datetime(das['Day'],format='%Y/%m/%d')\n",
    "das.set_index('Day', inplace = True)\n",
    "das.sort_values(by = ['Day'],axis=0, ascending=True)\n",
    "das"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "29c11f25",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Preclose</th>\n",
       "      <th>Open</th>\n",
       "      <th>Highest</th>\n",
       "      <th>Lowest</th>\n",
       "      <th>Close</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Day</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2025-01-02</th>\n",
       "      <td>3351.763</td>\n",
       "      <td>3347.939</td>\n",
       "      <td>3351.722</td>\n",
       "      <td>3242.087</td>\n",
       "      <td>3262.561</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-01-03</th>\n",
       "      <td>3262.561</td>\n",
       "      <td>3267.077</td>\n",
       "      <td>3273.566</td>\n",
       "      <td>3205.776</td>\n",
       "      <td>3211.430</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-01-06</th>\n",
       "      <td>3211.430</td>\n",
       "      <td>3209.783</td>\n",
       "      <td>3219.488</td>\n",
       "      <td>3185.463</td>\n",
       "      <td>3206.923</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-01-07</th>\n",
       "      <td>3206.923</td>\n",
       "      <td>3203.307</td>\n",
       "      <td>3230.853</td>\n",
       "      <td>3190.461</td>\n",
       "      <td>3229.644</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-01-08</th>\n",
       "      <td>3229.644</td>\n",
       "      <td>3218.858</td>\n",
       "      <td>3246.291</td>\n",
       "      <td>3175.725</td>\n",
       "      <td>3230.168</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-08-25</th>\n",
       "      <td>3825.759</td>\n",
       "      <td>3848.163</td>\n",
       "      <td>3883.562</td>\n",
       "      <td>3839.972</td>\n",
       "      <td>3883.562</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-08-26</th>\n",
       "      <td>3883.562</td>\n",
       "      <td>3871.471</td>\n",
       "      <td>3888.599</td>\n",
       "      <td>3859.758</td>\n",
       "      <td>3868.382</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-08-27</th>\n",
       "      <td>3868.382</td>\n",
       "      <td>3869.612</td>\n",
       "      <td>3887.198</td>\n",
       "      <td>3800.350</td>\n",
       "      <td>3800.350</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-08-28</th>\n",
       "      <td>3800.350</td>\n",
       "      <td>3796.711</td>\n",
       "      <td>3845.087</td>\n",
       "      <td>3761.422</td>\n",
       "      <td>3843.597</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-08-29</th>\n",
       "      <td>3843.597</td>\n",
       "      <td>3842.823</td>\n",
       "      <td>3867.606</td>\n",
       "      <td>3839.206</td>\n",
       "      <td>3857.927</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>161 rows × 5 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "            Preclose      Open   Highest    Lowest     Close\n",
       "Day                                                         \n",
       "2025-01-02  3351.763  3347.939  3351.722  3242.087  3262.561\n",
       "2025-01-03  3262.561  3267.077  3273.566  3205.776  3211.430\n",
       "2025-01-06  3211.430  3209.783  3219.488  3185.463  3206.923\n",
       "2025-01-07  3206.923  3203.307  3230.853  3190.461  3229.644\n",
       "2025-01-08  3229.644  3218.858  3246.291  3175.725  3230.168\n",
       "...              ...       ...       ...       ...       ...\n",
       "2025-08-25  3825.759  3848.163  3883.562  3839.972  3883.562\n",
       "2025-08-26  3883.562  3871.471  3888.599  3859.758  3868.382\n",
       "2025-08-27  3868.382  3869.612  3887.198  3800.350  3800.350\n",
       "2025-08-28  3800.350  3796.711  3845.087  3761.422  3843.597\n",
       "2025-08-29  3843.597  3842.823  3867.606  3839.206  3857.927\n",
       "\n",
       "[161 rows x 5 columns]"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data_new = das['2025-01':'2025-08'].copy()\n",
    "data_new['Close'] = pd.to_numeric(data_new['Close'])\n",
    "data_new['Preclose'] = pd.to_numeric(data_new['Preclose'])\n",
    "data_new"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "50a73f80",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Preclose</th>\n",
       "      <th>Open</th>\n",
       "      <th>Highest</th>\n",
       "      <th>Lowest</th>\n",
       "      <th>Close</th>\n",
       "      <th>Raw_return</th>\n",
       "      <th>Log_return</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Day</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2025-01-02</th>\n",
       "      <td>3351.763</td>\n",
       "      <td>3347.939</td>\n",
       "      <td>3351.722</td>\n",
       "      <td>3242.087</td>\n",
       "      <td>3262.561</td>\n",
       "      <td>-0.026613</td>\n",
       "      <td>-0.026974</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-01-03</th>\n",
       "      <td>3262.561</td>\n",
       "      <td>3267.077</td>\n",
       "      <td>3273.566</td>\n",
       "      <td>3205.776</td>\n",
       "      <td>3211.430</td>\n",
       "      <td>-0.015672</td>\n",
       "      <td>-0.015796</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-01-06</th>\n",
       "      <td>3211.430</td>\n",
       "      <td>3209.783</td>\n",
       "      <td>3219.488</td>\n",
       "      <td>3185.463</td>\n",
       "      <td>3206.923</td>\n",
       "      <td>-0.001403</td>\n",
       "      <td>-0.001404</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-01-07</th>\n",
       "      <td>3206.923</td>\n",
       "      <td>3203.307</td>\n",
       "      <td>3230.853</td>\n",
       "      <td>3190.461</td>\n",
       "      <td>3229.644</td>\n",
       "      <td>0.007085</td>\n",
       "      <td>0.007060</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-01-08</th>\n",
       "      <td>3229.644</td>\n",
       "      <td>3218.858</td>\n",
       "      <td>3246.291</td>\n",
       "      <td>3175.725</td>\n",
       "      <td>3230.168</td>\n",
       "      <td>0.000162</td>\n",
       "      <td>0.000162</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-08-25</th>\n",
       "      <td>3825.759</td>\n",
       "      <td>3848.163</td>\n",
       "      <td>3883.562</td>\n",
       "      <td>3839.972</td>\n",
       "      <td>3883.562</td>\n",
       "      <td>0.015109</td>\n",
       "      <td>0.014996</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-08-26</th>\n",
       "      <td>3883.562</td>\n",
       "      <td>3871.471</td>\n",
       "      <td>3888.599</td>\n",
       "      <td>3859.758</td>\n",
       "      <td>3868.382</td>\n",
       "      <td>-0.003909</td>\n",
       "      <td>-0.003916</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-08-27</th>\n",
       "      <td>3868.382</td>\n",
       "      <td>3869.612</td>\n",
       "      <td>3887.198</td>\n",
       "      <td>3800.350</td>\n",
       "      <td>3800.350</td>\n",
       "      <td>-0.017587</td>\n",
       "      <td>-0.017743</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-08-28</th>\n",
       "      <td>3800.350</td>\n",
       "      <td>3796.711</td>\n",
       "      <td>3845.087</td>\n",
       "      <td>3761.422</td>\n",
       "      <td>3843.597</td>\n",
       "      <td>0.011380</td>\n",
       "      <td>0.011315</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-08-29</th>\n",
       "      <td>3843.597</td>\n",
       "      <td>3842.823</td>\n",
       "      <td>3867.606</td>\n",
       "      <td>3839.206</td>\n",
       "      <td>3857.927</td>\n",
       "      <td>0.003728</td>\n",
       "      <td>0.003721</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>161 rows × 7 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "            Preclose      Open   Highest    Lowest     Close  Raw_return  \\\n",
       "Day                                                                        \n",
       "2025-01-02  3351.763  3347.939  3351.722  3242.087  3262.561   -0.026613   \n",
       "2025-01-03  3262.561  3267.077  3273.566  3205.776  3211.430   -0.015672   \n",
       "2025-01-06  3211.430  3209.783  3219.488  3185.463  3206.923   -0.001403   \n",
       "2025-01-07  3206.923  3203.307  3230.853  3190.461  3229.644    0.007085   \n",
       "2025-01-08  3229.644  3218.858  3246.291  3175.725  3230.168    0.000162   \n",
       "...              ...       ...       ...       ...       ...         ...   \n",
       "2025-08-25  3825.759  3848.163  3883.562  3839.972  3883.562    0.015109   \n",
       "2025-08-26  3883.562  3871.471  3888.599  3859.758  3868.382   -0.003909   \n",
       "2025-08-27  3868.382  3869.612  3887.198  3800.350  3800.350   -0.017587   \n",
       "2025-08-28  3800.350  3796.711  3845.087  3761.422  3843.597    0.011380   \n",
       "2025-08-29  3843.597  3842.823  3867.606  3839.206  3857.927    0.003728   \n",
       "\n",
       "            Log_return  \n",
       "Day                     \n",
       "2025-01-02   -0.026974  \n",
       "2025-01-03   -0.015796  \n",
       "2025-01-06   -0.001404  \n",
       "2025-01-07    0.007060  \n",
       "2025-01-08    0.000162  \n",
       "...                ...  \n",
       "2025-08-25    0.014996  \n",
       "2025-08-26   -0.003916  \n",
       "2025-08-27   -0.017743  \n",
       "2025-08-28    0.011315  \n",
       "2025-08-29    0.003721  \n",
       "\n",
       "[161 rows x 7 columns]"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 计算000001上证指数日收益率 - 方法1：直接使用向量化操作（最推荐的方式）\n",
    "data_new['Raw_return'] = data_new['Close'] / data_new['Preclose'] - 1\n",
    "data_new['Log_return'] = np.log(data_new['Close']) - np.log(data_new['Preclose'])\n",
    "\n",
    "data_new"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "4e22b9fb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Preclose</th>\n",
       "      <th>Open</th>\n",
       "      <th>Highest</th>\n",
       "      <th>Lowest</th>\n",
       "      <th>Close</th>\n",
       "      <th>Raw_return</th>\n",
       "      <th>Log_return</th>\n",
       "      <th>Pct_change_return</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Day</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2025-01-02</th>\n",
       "      <td>3351.763</td>\n",
       "      <td>3347.939</td>\n",
       "      <td>3351.722</td>\n",
       "      <td>3242.087</td>\n",
       "      <td>3262.561</td>\n",
       "      <td>-0.026613</td>\n",
       "      <td>-0.026974</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-01-03</th>\n",
       "      <td>3262.561</td>\n",
       "      <td>3267.077</td>\n",
       "      <td>3273.566</td>\n",
       "      <td>3205.776</td>\n",
       "      <td>3211.430</td>\n",
       "      <td>-0.015672</td>\n",
       "      <td>-0.015796</td>\n",
       "      <td>-0.015672</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-01-06</th>\n",
       "      <td>3211.430</td>\n",
       "      <td>3209.783</td>\n",
       "      <td>3219.488</td>\n",
       "      <td>3185.463</td>\n",
       "      <td>3206.923</td>\n",
       "      <td>-0.001403</td>\n",
       "      <td>-0.001404</td>\n",
       "      <td>-0.001403</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-01-07</th>\n",
       "      <td>3206.923</td>\n",
       "      <td>3203.307</td>\n",
       "      <td>3230.853</td>\n",
       "      <td>3190.461</td>\n",
       "      <td>3229.644</td>\n",
       "      <td>0.007085</td>\n",
       "      <td>0.007060</td>\n",
       "      <td>0.007085</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-01-08</th>\n",
       "      <td>3229.644</td>\n",
       "      <td>3218.858</td>\n",
       "      <td>3246.291</td>\n",
       "      <td>3175.725</td>\n",
       "      <td>3230.168</td>\n",
       "      <td>0.000162</td>\n",
       "      <td>0.000162</td>\n",
       "      <td>0.000162</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-08-25</th>\n",
       "      <td>3825.759</td>\n",
       "      <td>3848.163</td>\n",
       "      <td>3883.562</td>\n",
       "      <td>3839.972</td>\n",
       "      <td>3883.562</td>\n",
       "      <td>0.015109</td>\n",
       "      <td>0.014996</td>\n",
       "      <td>0.015109</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-08-26</th>\n",
       "      <td>3883.562</td>\n",
       "      <td>3871.471</td>\n",
       "      <td>3888.599</td>\n",
       "      <td>3859.758</td>\n",
       "      <td>3868.382</td>\n",
       "      <td>-0.003909</td>\n",
       "      <td>-0.003916</td>\n",
       "      <td>-0.003909</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-08-27</th>\n",
       "      <td>3868.382</td>\n",
       "      <td>3869.612</td>\n",
       "      <td>3887.198</td>\n",
       "      <td>3800.350</td>\n",
       "      <td>3800.350</td>\n",
       "      <td>-0.017587</td>\n",
       "      <td>-0.017743</td>\n",
       "      <td>-0.017587</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-08-28</th>\n",
       "      <td>3800.350</td>\n",
       "      <td>3796.711</td>\n",
       "      <td>3845.087</td>\n",
       "      <td>3761.422</td>\n",
       "      <td>3843.597</td>\n",
       "      <td>0.011380</td>\n",
       "      <td>0.011315</td>\n",
       "      <td>0.011380</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-08-29</th>\n",
       "      <td>3843.597</td>\n",
       "      <td>3842.823</td>\n",
       "      <td>3867.606</td>\n",
       "      <td>3839.206</td>\n",
       "      <td>3857.927</td>\n",
       "      <td>0.003728</td>\n",
       "      <td>0.003721</td>\n",
       "      <td>0.003728</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>161 rows × 8 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "            Preclose      Open   Highest    Lowest     Close  Raw_return  \\\n",
       "Day                                                                        \n",
       "2025-01-02  3351.763  3347.939  3351.722  3242.087  3262.561   -0.026613   \n",
       "2025-01-03  3262.561  3267.077  3273.566  3205.776  3211.430   -0.015672   \n",
       "2025-01-06  3211.430  3209.783  3219.488  3185.463  3206.923   -0.001403   \n",
       "2025-01-07  3206.923  3203.307  3230.853  3190.461  3229.644    0.007085   \n",
       "2025-01-08  3229.644  3218.858  3246.291  3175.725  3230.168    0.000162   \n",
       "...              ...       ...       ...       ...       ...         ...   \n",
       "2025-08-25  3825.759  3848.163  3883.562  3839.972  3883.562    0.015109   \n",
       "2025-08-26  3883.562  3871.471  3888.599  3859.758  3868.382   -0.003909   \n",
       "2025-08-27  3868.382  3869.612  3887.198  3800.350  3800.350   -0.017587   \n",
       "2025-08-28  3800.350  3796.711  3845.087  3761.422  3843.597    0.011380   \n",
       "2025-08-29  3843.597  3842.823  3867.606  3839.206  3857.927    0.003728   \n",
       "\n",
       "            Log_return  Pct_change_return  \n",
       "Day                                        \n",
       "2025-01-02   -0.026974                NaN  \n",
       "2025-01-03   -0.015796          -0.015672  \n",
       "2025-01-06   -0.001404          -0.001403  \n",
       "2025-01-07    0.007060           0.007085  \n",
       "2025-01-08    0.000162           0.000162  \n",
       "...                ...                ...  \n",
       "2025-08-25    0.014996           0.015109  \n",
       "2025-08-26   -0.003916          -0.003909  \n",
       "2025-08-27   -0.017743          -0.017587  \n",
       "2025-08-28    0.011315           0.011380  \n",
       "2025-08-29    0.003721           0.003728  \n",
       "\n",
       "[161 rows x 8 columns]"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#方法2：使用pandas的pct_change函数计算收益率（适用于时间序列数据）\n",
    "# 注意：这种方法需要数据已经按时间排序\n",
    "data_new['Pct_change_return'] = data_new['Close'].pct_change()\n",
    "data_new"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "edf0757b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Preclose</th>\n",
       "      <th>Open</th>\n",
       "      <th>Highest</th>\n",
       "      <th>Lowest</th>\n",
       "      <th>Close</th>\n",
       "      <th>Raw_return</th>\n",
       "      <th>Log_return</th>\n",
       "      <th>Pct_change_return</th>\n",
       "      <th>Apply_return</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Day</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2025-01-02</th>\n",
       "      <td>3351.763</td>\n",
       "      <td>3347.939</td>\n",
       "      <td>3351.722</td>\n",
       "      <td>3242.087</td>\n",
       "      <td>3262.561</td>\n",
       "      <td>-0.026613</td>\n",
       "      <td>-0.026974</td>\n",
       "      <td>NaN</td>\n",
       "      <td>-0.026613</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-01-03</th>\n",
       "      <td>3262.561</td>\n",
       "      <td>3267.077</td>\n",
       "      <td>3273.566</td>\n",
       "      <td>3205.776</td>\n",
       "      <td>3211.430</td>\n",
       "      <td>-0.015672</td>\n",
       "      <td>-0.015796</td>\n",
       "      <td>-0.015672</td>\n",
       "      <td>-0.015672</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-01-06</th>\n",
       "      <td>3211.430</td>\n",
       "      <td>3209.783</td>\n",
       "      <td>3219.488</td>\n",
       "      <td>3185.463</td>\n",
       "      <td>3206.923</td>\n",
       "      <td>-0.001403</td>\n",
       "      <td>-0.001404</td>\n",
       "      <td>-0.001403</td>\n",
       "      <td>-0.001403</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-01-07</th>\n",
       "      <td>3206.923</td>\n",
       "      <td>3203.307</td>\n",
       "      <td>3230.853</td>\n",
       "      <td>3190.461</td>\n",
       "      <td>3229.644</td>\n",
       "      <td>0.007085</td>\n",
       "      <td>0.007060</td>\n",
       "      <td>0.007085</td>\n",
       "      <td>0.007085</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-01-08</th>\n",
       "      <td>3229.644</td>\n",
       "      <td>3218.858</td>\n",
       "      <td>3246.291</td>\n",
       "      <td>3175.725</td>\n",
       "      <td>3230.168</td>\n",
       "      <td>0.000162</td>\n",
       "      <td>0.000162</td>\n",
       "      <td>0.000162</td>\n",
       "      <td>0.000162</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-08-25</th>\n",
       "      <td>3825.759</td>\n",
       "      <td>3848.163</td>\n",
       "      <td>3883.562</td>\n",
       "      <td>3839.972</td>\n",
       "      <td>3883.562</td>\n",
       "      <td>0.015109</td>\n",
       "      <td>0.014996</td>\n",
       "      <td>0.015109</td>\n",
       "      <td>0.015109</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-08-26</th>\n",
       "      <td>3883.562</td>\n",
       "      <td>3871.471</td>\n",
       "      <td>3888.599</td>\n",
       "      <td>3859.758</td>\n",
       "      <td>3868.382</td>\n",
       "      <td>-0.003909</td>\n",
       "      <td>-0.003916</td>\n",
       "      <td>-0.003909</td>\n",
       "      <td>-0.003909</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-08-27</th>\n",
       "      <td>3868.382</td>\n",
       "      <td>3869.612</td>\n",
       "      <td>3887.198</td>\n",
       "      <td>3800.350</td>\n",
       "      <td>3800.350</td>\n",
       "      <td>-0.017587</td>\n",
       "      <td>-0.017743</td>\n",
       "      <td>-0.017587</td>\n",
       "      <td>-0.017587</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-08-28</th>\n",
       "      <td>3800.350</td>\n",
       "      <td>3796.711</td>\n",
       "      <td>3845.087</td>\n",
       "      <td>3761.422</td>\n",
       "      <td>3843.597</td>\n",
       "      <td>0.011380</td>\n",
       "      <td>0.011315</td>\n",
       "      <td>0.011380</td>\n",
       "      <td>0.011380</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-08-29</th>\n",
       "      <td>3843.597</td>\n",
       "      <td>3842.823</td>\n",
       "      <td>3867.606</td>\n",
       "      <td>3839.206</td>\n",
       "      <td>3857.927</td>\n",
       "      <td>0.003728</td>\n",
       "      <td>0.003721</td>\n",
       "      <td>0.003728</td>\n",
       "      <td>0.003728</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>161 rows × 9 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "            Preclose      Open   Highest    Lowest     Close  Raw_return  \\\n",
       "Day                                                                        \n",
       "2025-01-02  3351.763  3347.939  3351.722  3242.087  3262.561   -0.026613   \n",
       "2025-01-03  3262.561  3267.077  3273.566  3205.776  3211.430   -0.015672   \n",
       "2025-01-06  3211.430  3209.783  3219.488  3185.463  3206.923   -0.001403   \n",
       "2025-01-07  3206.923  3203.307  3230.853  3190.461  3229.644    0.007085   \n",
       "2025-01-08  3229.644  3218.858  3246.291  3175.725  3230.168    0.000162   \n",
       "...              ...       ...       ...       ...       ...         ...   \n",
       "2025-08-25  3825.759  3848.163  3883.562  3839.972  3883.562    0.015109   \n",
       "2025-08-26  3883.562  3871.471  3888.599  3859.758  3868.382   -0.003909   \n",
       "2025-08-27  3868.382  3869.612  3887.198  3800.350  3800.350   -0.017587   \n",
       "2025-08-28  3800.350  3796.711  3845.087  3761.422  3843.597    0.011380   \n",
       "2025-08-29  3843.597  3842.823  3867.606  3839.206  3857.927    0.003728   \n",
       "\n",
       "            Log_return  Pct_change_return  Apply_return  \n",
       "Day                                                      \n",
       "2025-01-02   -0.026974                NaN     -0.026613  \n",
       "2025-01-03   -0.015796          -0.015672     -0.015672  \n",
       "2025-01-06   -0.001404          -0.001403     -0.001403  \n",
       "2025-01-07    0.007060           0.007085      0.007085  \n",
       "2025-01-08    0.000162           0.000162      0.000162  \n",
       "...                ...                ...           ...  \n",
       "2025-08-25    0.014996           0.015109      0.015109  \n",
       "2025-08-26   -0.003916          -0.003909     -0.003909  \n",
       "2025-08-27   -0.017743          -0.017587     -0.017587  \n",
       "2025-08-28    0.011315           0.011380      0.011380  \n",
       "2025-08-29    0.003721           0.003728      0.003728  \n",
       "\n",
       "[161 rows x 9 columns]"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 方法3：使用apply方法（不推荐，因为速度较慢）\n",
    "data_new['Apply_return'] = data_new.apply(lambda row: row['Close'] / row['Preclose'] - 1, axis=1)\n",
    "data_new"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "09129761",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Preclose</th>\n",
       "      <th>Open</th>\n",
       "      <th>Highest</th>\n",
       "      <th>Lowest</th>\n",
       "      <th>Close</th>\n",
       "      <th>Raw_return</th>\n",
       "      <th>Log_return</th>\n",
       "      <th>Pct_change_return</th>\n",
       "      <th>Apply_return</th>\n",
       "      <th>Diff_div_return</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Day</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2025-01-02</th>\n",
       "      <td>3351.763</td>\n",
       "      <td>3347.939</td>\n",
       "      <td>3351.722</td>\n",
       "      <td>3242.087</td>\n",
       "      <td>3262.561</td>\n",
       "      <td>-0.026613</td>\n",
       "      <td>-0.026974</td>\n",
       "      <td>NaN</td>\n",
       "      <td>-0.026613</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-01-03</th>\n",
       "      <td>3262.561</td>\n",
       "      <td>3267.077</td>\n",
       "      <td>3273.566</td>\n",
       "      <td>3205.776</td>\n",
       "      <td>3211.430</td>\n",
       "      <td>-0.015672</td>\n",
       "      <td>-0.015796</td>\n",
       "      <td>-0.015672</td>\n",
       "      <td>-0.015672</td>\n",
       "      <td>-0.015672</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-01-06</th>\n",
       "      <td>3211.430</td>\n",
       "      <td>3209.783</td>\n",
       "      <td>3219.488</td>\n",
       "      <td>3185.463</td>\n",
       "      <td>3206.923</td>\n",
       "      <td>-0.001403</td>\n",
       "      <td>-0.001404</td>\n",
       "      <td>-0.001403</td>\n",
       "      <td>-0.001403</td>\n",
       "      <td>-0.001403</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-01-07</th>\n",
       "      <td>3206.923</td>\n",
       "      <td>3203.307</td>\n",
       "      <td>3230.853</td>\n",
       "      <td>3190.461</td>\n",
       "      <td>3229.644</td>\n",
       "      <td>0.007085</td>\n",
       "      <td>0.007060</td>\n",
       "      <td>0.007085</td>\n",
       "      <td>0.007085</td>\n",
       "      <td>0.007085</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-01-08</th>\n",
       "      <td>3229.644</td>\n",
       "      <td>3218.858</td>\n",
       "      <td>3246.291</td>\n",
       "      <td>3175.725</td>\n",
       "      <td>3230.168</td>\n",
       "      <td>0.000162</td>\n",
       "      <td>0.000162</td>\n",
       "      <td>0.000162</td>\n",
       "      <td>0.000162</td>\n",
       "      <td>0.000162</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-08-25</th>\n",
       "      <td>3825.759</td>\n",
       "      <td>3848.163</td>\n",
       "      <td>3883.562</td>\n",
       "      <td>3839.972</td>\n",
       "      <td>3883.562</td>\n",
       "      <td>0.015109</td>\n",
       "      <td>0.014996</td>\n",
       "      <td>0.015109</td>\n",
       "      <td>0.015109</td>\n",
       "      <td>0.015109</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-08-26</th>\n",
       "      <td>3883.562</td>\n",
       "      <td>3871.471</td>\n",
       "      <td>3888.599</td>\n",
       "      <td>3859.758</td>\n",
       "      <td>3868.382</td>\n",
       "      <td>-0.003909</td>\n",
       "      <td>-0.003916</td>\n",
       "      <td>-0.003909</td>\n",
       "      <td>-0.003909</td>\n",
       "      <td>-0.003909</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-08-27</th>\n",
       "      <td>3868.382</td>\n",
       "      <td>3869.612</td>\n",
       "      <td>3887.198</td>\n",
       "      <td>3800.350</td>\n",
       "      <td>3800.350</td>\n",
       "      <td>-0.017587</td>\n",
       "      <td>-0.017743</td>\n",
       "      <td>-0.017587</td>\n",
       "      <td>-0.017587</td>\n",
       "      <td>-0.017587</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-08-28</th>\n",
       "      <td>3800.350</td>\n",
       "      <td>3796.711</td>\n",
       "      <td>3845.087</td>\n",
       "      <td>3761.422</td>\n",
       "      <td>3843.597</td>\n",
       "      <td>0.011380</td>\n",
       "      <td>0.011315</td>\n",
       "      <td>0.011380</td>\n",
       "      <td>0.011380</td>\n",
       "      <td>0.011380</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-08-29</th>\n",
       "      <td>3843.597</td>\n",
       "      <td>3842.823</td>\n",
       "      <td>3867.606</td>\n",
       "      <td>3839.206</td>\n",
       "      <td>3857.927</td>\n",
       "      <td>0.003728</td>\n",
       "      <td>0.003721</td>\n",
       "      <td>0.003728</td>\n",
       "      <td>0.003728</td>\n",
       "      <td>0.003728</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>161 rows × 10 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "            Preclose      Open   Highest    Lowest     Close  Raw_return  \\\n",
       "Day                                                                        \n",
       "2025-01-02  3351.763  3347.939  3351.722  3242.087  3262.561   -0.026613   \n",
       "2025-01-03  3262.561  3267.077  3273.566  3205.776  3211.430   -0.015672   \n",
       "2025-01-06  3211.430  3209.783  3219.488  3185.463  3206.923   -0.001403   \n",
       "2025-01-07  3206.923  3203.307  3230.853  3190.461  3229.644    0.007085   \n",
       "2025-01-08  3229.644  3218.858  3246.291  3175.725  3230.168    0.000162   \n",
       "...              ...       ...       ...       ...       ...         ...   \n",
       "2025-08-25  3825.759  3848.163  3883.562  3839.972  3883.562    0.015109   \n",
       "2025-08-26  3883.562  3871.471  3888.599  3859.758  3868.382   -0.003909   \n",
       "2025-08-27  3868.382  3869.612  3887.198  3800.350  3800.350   -0.017587   \n",
       "2025-08-28  3800.350  3796.711  3845.087  3761.422  3843.597    0.011380   \n",
       "2025-08-29  3843.597  3842.823  3867.606  3839.206  3857.927    0.003728   \n",
       "\n",
       "            Log_return  Pct_change_return  Apply_return  Diff_div_return  \n",
       "Day                                                                       \n",
       "2025-01-02   -0.026974                NaN     -0.026613              NaN  \n",
       "2025-01-03   -0.015796          -0.015672     -0.015672        -0.015672  \n",
       "2025-01-06   -0.001404          -0.001403     -0.001403        -0.001403  \n",
       "2025-01-07    0.007060           0.007085      0.007085         0.007085  \n",
       "2025-01-08    0.000162           0.000162      0.000162         0.000162  \n",
       "...                ...                ...           ...              ...  \n",
       "2025-08-25    0.014996           0.015109      0.015109         0.015109  \n",
       "2025-08-26   -0.003916          -0.003909     -0.003909        -0.003909  \n",
       "2025-08-27   -0.017743          -0.017587     -0.017587        -0.017587  \n",
       "2025-08-28    0.011315           0.011380      0.011380         0.011380  \n",
       "2025-08-29    0.003721           0.003728      0.003728         0.003728  \n",
       "\n",
       "[161 rows x 10 columns]"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 方法4：使用diff和div方法组合（另一种向量化操作）\n",
    "data_new['Diff_div_return'] = data_new['Close'].diff() / data_new['Close'].shift(1)\n",
    "data_new"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "7aaf1419",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Preclose</th>\n",
       "      <th>Open</th>\n",
       "      <th>Highest</th>\n",
       "      <th>Lowest</th>\n",
       "      <th>Close</th>\n",
       "      <th>Raw_return</th>\n",
       "      <th>Log_return</th>\n",
       "      <th>Pct_change_return</th>\n",
       "      <th>Apply_return</th>\n",
       "      <th>Diff_div_return</th>\n",
       "      <th>Loop_return</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Day</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2025-01-02</th>\n",
       "      <td>3351.763</td>\n",
       "      <td>3347.939</td>\n",
       "      <td>3351.722</td>\n",
       "      <td>3242.087</td>\n",
       "      <td>3262.561</td>\n",
       "      <td>-0.026613</td>\n",
       "      <td>-0.026974</td>\n",
       "      <td>NaN</td>\n",
       "      <td>-0.026613</td>\n",
       "      <td>NaN</td>\n",
       "      <td>-0.026613</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-01-03</th>\n",
       "      <td>3262.561</td>\n",
       "      <td>3267.077</td>\n",
       "      <td>3273.566</td>\n",
       "      <td>3205.776</td>\n",
       "      <td>3211.430</td>\n",
       "      <td>-0.015672</td>\n",
       "      <td>-0.015796</td>\n",
       "      <td>-0.015672</td>\n",
       "      <td>-0.015672</td>\n",
       "      <td>-0.015672</td>\n",
       "      <td>-0.015672</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-01-06</th>\n",
       "      <td>3211.430</td>\n",
       "      <td>3209.783</td>\n",
       "      <td>3219.488</td>\n",
       "      <td>3185.463</td>\n",
       "      <td>3206.923</td>\n",
       "      <td>-0.001403</td>\n",
       "      <td>-0.001404</td>\n",
       "      <td>-0.001403</td>\n",
       "      <td>-0.001403</td>\n",
       "      <td>-0.001403</td>\n",
       "      <td>-0.001403</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-01-07</th>\n",
       "      <td>3206.923</td>\n",
       "      <td>3203.307</td>\n",
       "      <td>3230.853</td>\n",
       "      <td>3190.461</td>\n",
       "      <td>3229.644</td>\n",
       "      <td>0.007085</td>\n",
       "      <td>0.007060</td>\n",
       "      <td>0.007085</td>\n",
       "      <td>0.007085</td>\n",
       "      <td>0.007085</td>\n",
       "      <td>0.007085</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-01-08</th>\n",
       "      <td>3229.644</td>\n",
       "      <td>3218.858</td>\n",
       "      <td>3246.291</td>\n",
       "      <td>3175.725</td>\n",
       "      <td>3230.168</td>\n",
       "      <td>0.000162</td>\n",
       "      <td>0.000162</td>\n",
       "      <td>0.000162</td>\n",
       "      <td>0.000162</td>\n",
       "      <td>0.000162</td>\n",
       "      <td>0.000162</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-08-25</th>\n",
       "      <td>3825.759</td>\n",
       "      <td>3848.163</td>\n",
       "      <td>3883.562</td>\n",
       "      <td>3839.972</td>\n",
       "      <td>3883.562</td>\n",
       "      <td>0.015109</td>\n",
       "      <td>0.014996</td>\n",
       "      <td>0.015109</td>\n",
       "      <td>0.015109</td>\n",
       "      <td>0.015109</td>\n",
       "      <td>0.015109</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-08-26</th>\n",
       "      <td>3883.562</td>\n",
       "      <td>3871.471</td>\n",
       "      <td>3888.599</td>\n",
       "      <td>3859.758</td>\n",
       "      <td>3868.382</td>\n",
       "      <td>-0.003909</td>\n",
       "      <td>-0.003916</td>\n",
       "      <td>-0.003909</td>\n",
       "      <td>-0.003909</td>\n",
       "      <td>-0.003909</td>\n",
       "      <td>-0.003909</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-08-27</th>\n",
       "      <td>3868.382</td>\n",
       "      <td>3869.612</td>\n",
       "      <td>3887.198</td>\n",
       "      <td>3800.350</td>\n",
       "      <td>3800.350</td>\n",
       "      <td>-0.017587</td>\n",
       "      <td>-0.017743</td>\n",
       "      <td>-0.017587</td>\n",
       "      <td>-0.017587</td>\n",
       "      <td>-0.017587</td>\n",
       "      <td>-0.017587</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-08-28</th>\n",
       "      <td>3800.350</td>\n",
       "      <td>3796.711</td>\n",
       "      <td>3845.087</td>\n",
       "      <td>3761.422</td>\n",
       "      <td>3843.597</td>\n",
       "      <td>0.011380</td>\n",
       "      <td>0.011315</td>\n",
       "      <td>0.011380</td>\n",
       "      <td>0.011380</td>\n",
       "      <td>0.011380</td>\n",
       "      <td>0.011380</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-08-29</th>\n",
       "      <td>3843.597</td>\n",
       "      <td>3842.823</td>\n",
       "      <td>3867.606</td>\n",
       "      <td>3839.206</td>\n",
       "      <td>3857.927</td>\n",
       "      <td>0.003728</td>\n",
       "      <td>0.003721</td>\n",
       "      <td>0.003728</td>\n",
       "      <td>0.003728</td>\n",
       "      <td>0.003728</td>\n",
       "      <td>0.003728</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>161 rows × 11 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "            Preclose      Open   Highest    Lowest     Close  Raw_return  \\\n",
       "Day                                                                        \n",
       "2025-01-02  3351.763  3347.939  3351.722  3242.087  3262.561   -0.026613   \n",
       "2025-01-03  3262.561  3267.077  3273.566  3205.776  3211.430   -0.015672   \n",
       "2025-01-06  3211.430  3209.783  3219.488  3185.463  3206.923   -0.001403   \n",
       "2025-01-07  3206.923  3203.307  3230.853  3190.461  3229.644    0.007085   \n",
       "2025-01-08  3229.644  3218.858  3246.291  3175.725  3230.168    0.000162   \n",
       "...              ...       ...       ...       ...       ...         ...   \n",
       "2025-08-25  3825.759  3848.163  3883.562  3839.972  3883.562    0.015109   \n",
       "2025-08-26  3883.562  3871.471  3888.599  3859.758  3868.382   -0.003909   \n",
       "2025-08-27  3868.382  3869.612  3887.198  3800.350  3800.350   -0.017587   \n",
       "2025-08-28  3800.350  3796.711  3845.087  3761.422  3843.597    0.011380   \n",
       "2025-08-29  3843.597  3842.823  3867.606  3839.206  3857.927    0.003728   \n",
       "\n",
       "            Log_return  Pct_change_return  Apply_return  Diff_div_return  \\\n",
       "Day                                                                        \n",
       "2025-01-02   -0.026974                NaN     -0.026613              NaN   \n",
       "2025-01-03   -0.015796          -0.015672     -0.015672        -0.015672   \n",
       "2025-01-06   -0.001404          -0.001403     -0.001403        -0.001403   \n",
       "2025-01-07    0.007060           0.007085      0.007085         0.007085   \n",
       "2025-01-08    0.000162           0.000162      0.000162         0.000162   \n",
       "...                ...                ...           ...              ...   \n",
       "2025-08-25    0.014996           0.015109      0.015109         0.015109   \n",
       "2025-08-26   -0.003916          -0.003909     -0.003909        -0.003909   \n",
       "2025-08-27   -0.017743          -0.017587     -0.017587        -0.017587   \n",
       "2025-08-28    0.011315           0.011380      0.011380         0.011380   \n",
       "2025-08-29    0.003721           0.003728      0.003728         0.003728   \n",
       "\n",
       "            Loop_return  \n",
       "Day                      \n",
       "2025-01-02    -0.026613  \n",
       "2025-01-03    -0.015672  \n",
       "2025-01-06    -0.001403  \n",
       "2025-01-07     0.007085  \n",
       "2025-01-08     0.000162  \n",
       "...                 ...  \n",
       "2025-08-25     0.015109  \n",
       "2025-08-26    -0.003909  \n",
       "2025-08-27    -0.017587  \n",
       "2025-08-28     0.011380  \n",
       "2025-08-29     0.003728  \n",
       "\n",
       "[161 rows x 11 columns]"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 创建新列存储结果\n",
    "if 'Loop_return' not in data_new.columns:\n",
    "    data_new['Loop_return'] = np.nan\n",
    "\n",
    "# 使用for循环计算\n",
    "for i in range(len(data_new)):\n",
    "    data_new.iloc[i, data_new.columns.get_loc('Loop_return')] = data_new.iloc[i, data_new.columns.get_loc('Close')] / data_new.iloc[i, data_new.columns.get_loc('Preclose')] - 1\n",
    "data_new"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "6e6e1fb8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Preclose</th>\n",
       "      <th>Open</th>\n",
       "      <th>Highest</th>\n",
       "      <th>Lowest</th>\n",
       "      <th>Close</th>\n",
       "      <th>Raw_return</th>\n",
       "      <th>Log_return</th>\n",
       "      <th>Pct_change_return</th>\n",
       "      <th>Apply_return</th>\n",
       "      <th>Diff_div_return</th>\n",
       "      <th>Loop_return</th>\n",
       "      <th>Numpy_return</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Day</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2025-01-02</th>\n",
       "      <td>3351.763</td>\n",
       "      <td>3347.939</td>\n",
       "      <td>3351.722</td>\n",
       "      <td>3242.087</td>\n",
       "      <td>3262.561</td>\n",
       "      <td>-0.026613</td>\n",
       "      <td>-0.026974</td>\n",
       "      <td>NaN</td>\n",
       "      <td>-0.026613</td>\n",
       "      <td>NaN</td>\n",
       "      <td>-0.026613</td>\n",
       "      <td>-0.026613</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-01-03</th>\n",
       "      <td>3262.561</td>\n",
       "      <td>3267.077</td>\n",
       "      <td>3273.566</td>\n",
       "      <td>3205.776</td>\n",
       "      <td>3211.430</td>\n",
       "      <td>-0.015672</td>\n",
       "      <td>-0.015796</td>\n",
       "      <td>-0.015672</td>\n",
       "      <td>-0.015672</td>\n",
       "      <td>-0.015672</td>\n",
       "      <td>-0.015672</td>\n",
       "      <td>-0.015672</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-01-06</th>\n",
       "      <td>3211.430</td>\n",
       "      <td>3209.783</td>\n",
       "      <td>3219.488</td>\n",
       "      <td>3185.463</td>\n",
       "      <td>3206.923</td>\n",
       "      <td>-0.001403</td>\n",
       "      <td>-0.001404</td>\n",
       "      <td>-0.001403</td>\n",
       "      <td>-0.001403</td>\n",
       "      <td>-0.001403</td>\n",
       "      <td>-0.001403</td>\n",
       "      <td>-0.001403</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-01-07</th>\n",
       "      <td>3206.923</td>\n",
       "      <td>3203.307</td>\n",
       "      <td>3230.853</td>\n",
       "      <td>3190.461</td>\n",
       "      <td>3229.644</td>\n",
       "      <td>0.007085</td>\n",
       "      <td>0.007060</td>\n",
       "      <td>0.007085</td>\n",
       "      <td>0.007085</td>\n",
       "      <td>0.007085</td>\n",
       "      <td>0.007085</td>\n",
       "      <td>0.007085</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-01-08</th>\n",
       "      <td>3229.644</td>\n",
       "      <td>3218.858</td>\n",
       "      <td>3246.291</td>\n",
       "      <td>3175.725</td>\n",
       "      <td>3230.168</td>\n",
       "      <td>0.000162</td>\n",
       "      <td>0.000162</td>\n",
       "      <td>0.000162</td>\n",
       "      <td>0.000162</td>\n",
       "      <td>0.000162</td>\n",
       "      <td>0.000162</td>\n",
       "      <td>0.000162</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-08-25</th>\n",
       "      <td>3825.759</td>\n",
       "      <td>3848.163</td>\n",
       "      <td>3883.562</td>\n",
       "      <td>3839.972</td>\n",
       "      <td>3883.562</td>\n",
       "      <td>0.015109</td>\n",
       "      <td>0.014996</td>\n",
       "      <td>0.015109</td>\n",
       "      <td>0.015109</td>\n",
       "      <td>0.015109</td>\n",
       "      <td>0.015109</td>\n",
       "      <td>0.015109</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-08-26</th>\n",
       "      <td>3883.562</td>\n",
       "      <td>3871.471</td>\n",
       "      <td>3888.599</td>\n",
       "      <td>3859.758</td>\n",
       "      <td>3868.382</td>\n",
       "      <td>-0.003909</td>\n",
       "      <td>-0.003916</td>\n",
       "      <td>-0.003909</td>\n",
       "      <td>-0.003909</td>\n",
       "      <td>-0.003909</td>\n",
       "      <td>-0.003909</td>\n",
       "      <td>-0.003909</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-08-27</th>\n",
       "      <td>3868.382</td>\n",
       "      <td>3869.612</td>\n",
       "      <td>3887.198</td>\n",
       "      <td>3800.350</td>\n",
       "      <td>3800.350</td>\n",
       "      <td>-0.017587</td>\n",
       "      <td>-0.017743</td>\n",
       "      <td>-0.017587</td>\n",
       "      <td>-0.017587</td>\n",
       "      <td>-0.017587</td>\n",
       "      <td>-0.017587</td>\n",
       "      <td>-0.017587</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-08-28</th>\n",
       "      <td>3800.350</td>\n",
       "      <td>3796.711</td>\n",
       "      <td>3845.087</td>\n",
       "      <td>3761.422</td>\n",
       "      <td>3843.597</td>\n",
       "      <td>0.011380</td>\n",
       "      <td>0.011315</td>\n",
       "      <td>0.011380</td>\n",
       "      <td>0.011380</td>\n",
       "      <td>0.011380</td>\n",
       "      <td>0.011380</td>\n",
       "      <td>0.011380</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-08-29</th>\n",
       "      <td>3843.597</td>\n",
       "      <td>3842.823</td>\n",
       "      <td>3867.606</td>\n",
       "      <td>3839.206</td>\n",
       "      <td>3857.927</td>\n",
       "      <td>0.003728</td>\n",
       "      <td>0.003721</td>\n",
       "      <td>0.003728</td>\n",
       "      <td>0.003728</td>\n",
       "      <td>0.003728</td>\n",
       "      <td>0.003728</td>\n",
       "      <td>0.003728</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>161 rows × 12 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "            Preclose      Open   Highest    Lowest     Close  Raw_return  \\\n",
       "Day                                                                        \n",
       "2025-01-02  3351.763  3347.939  3351.722  3242.087  3262.561   -0.026613   \n",
       "2025-01-03  3262.561  3267.077  3273.566  3205.776  3211.430   -0.015672   \n",
       "2025-01-06  3211.430  3209.783  3219.488  3185.463  3206.923   -0.001403   \n",
       "2025-01-07  3206.923  3203.307  3230.853  3190.461  3229.644    0.007085   \n",
       "2025-01-08  3229.644  3218.858  3246.291  3175.725  3230.168    0.000162   \n",
       "...              ...       ...       ...       ...       ...         ...   \n",
       "2025-08-25  3825.759  3848.163  3883.562  3839.972  3883.562    0.015109   \n",
       "2025-08-26  3883.562  3871.471  3888.599  3859.758  3868.382   -0.003909   \n",
       "2025-08-27  3868.382  3869.612  3887.198  3800.350  3800.350   -0.017587   \n",
       "2025-08-28  3800.350  3796.711  3845.087  3761.422  3843.597    0.011380   \n",
       "2025-08-29  3843.597  3842.823  3867.606  3839.206  3857.927    0.003728   \n",
       "\n",
       "            Log_return  Pct_change_return  Apply_return  Diff_div_return  \\\n",
       "Day                                                                        \n",
       "2025-01-02   -0.026974                NaN     -0.026613              NaN   \n",
       "2025-01-03   -0.015796          -0.015672     -0.015672        -0.015672   \n",
       "2025-01-06   -0.001404          -0.001403     -0.001403        -0.001403   \n",
       "2025-01-07    0.007060           0.007085      0.007085         0.007085   \n",
       "2025-01-08    0.000162           0.000162      0.000162         0.000162   \n",
       "...                ...                ...           ...              ...   \n",
       "2025-08-25    0.014996           0.015109      0.015109         0.015109   \n",
       "2025-08-26   -0.003916          -0.003909     -0.003909        -0.003909   \n",
       "2025-08-27   -0.017743          -0.017587     -0.017587        -0.017587   \n",
       "2025-08-28    0.011315           0.011380      0.011380         0.011380   \n",
       "2025-08-29    0.003721           0.003728      0.003728         0.003728   \n",
       "\n",
       "            Loop_return  Numpy_return  \n",
       "Day                                    \n",
       "2025-01-02    -0.026613     -0.026613  \n",
       "2025-01-03    -0.015672     -0.015672  \n",
       "2025-01-06    -0.001403     -0.001403  \n",
       "2025-01-07     0.007085      0.007085  \n",
       "2025-01-08     0.000162      0.000162  \n",
       "...                 ...           ...  \n",
       "2025-08-25     0.015109      0.015109  \n",
       "2025-08-26    -0.003909     -0.003909  \n",
       "2025-08-27    -0.017587     -0.017587  \n",
       "2025-08-28     0.011380      0.011380  \n",
       "2025-08-29     0.003728      0.003728  \n",
       "\n",
       "[161 rows x 12 columns]"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#使用numpy的向量化操作（高效且简洁）\n",
    "data_new['Numpy_return'] = (data_new['Close'].values / data_new['Preclose'].values) - 1\n",
    "\n",
    "# 显示结果\n",
    "data_new"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "81fb0d8b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Log_return</th>\n",
       "      <th>Raw_Return</th>\n",
       "      <th>Year</th>\n",
       "      <th>Month</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Day</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2025-01-31</th>\n",
       "      <td>-0.030647</td>\n",
       "      <td>-0.030182</td>\n",
       "      <td>2025</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-02-28</th>\n",
       "      <td>0.021395</td>\n",
       "      <td>0.021626</td>\n",
       "      <td>2025</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-03-31</th>\n",
       "      <td>0.004461</td>\n",
       "      <td>0.004471</td>\n",
       "      <td>2025</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-04-30</th>\n",
       "      <td>-0.017148</td>\n",
       "      <td>-0.017002</td>\n",
       "      <td>2025</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-05-31</th>\n",
       "      <td>0.020662</td>\n",
       "      <td>0.020877</td>\n",
       "      <td>2025</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "            Log_return  Raw_Return  Year  Month\n",
       "Day                                            \n",
       "2025-01-31   -0.030647   -0.030182  2025      1\n",
       "2025-02-28    0.021395    0.021626  2025      2\n",
       "2025-03-31    0.004461    0.004471  2025      3\n",
       "2025-04-30   -0.017148   -0.017002  2025      4\n",
       "2025-05-31    0.020662    0.020877  2025      5"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 方法1：使用resample函数计算月度对数收益率并转换为原始收益率\n",
    "# 这种方法适合对数收益率，因为对数收益率可以直接相加\n",
    "Month_data1 = data_new.resample('ME')['Log_return'].sum().to_frame(name='Log_return') \n",
    "Month_data1['Raw_Return'] = np.exp(Month_data1['Log_return']) - 1\n",
    "\n",
    "# 添加年月信息便于分析\n",
    "Month_data1['Year'] = Month_data1.index.year\n",
    "Month_data1['Month'] = Month_data1.index.month\n",
    "\n",
    "# 显示结果\n",
    "Month_data1.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "eb396449",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Close</th>\n",
       "      <th>Preclose</th>\n",
       "      <th>Raw_return</th>\n",
       "      <th>Log_return</th>\n",
       "      <th>Year</th>\n",
       "      <th>Month</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Day</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2025-01-31</th>\n",
       "      <td>3250.601</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>2025</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-02-28</th>\n",
       "      <td>3320.897</td>\n",
       "      <td>3250.601</td>\n",
       "      <td>0.021626</td>\n",
       "      <td>0.021395</td>\n",
       "      <td>2025</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-03-31</th>\n",
       "      <td>3335.746</td>\n",
       "      <td>3320.897</td>\n",
       "      <td>0.004471</td>\n",
       "      <td>0.004461</td>\n",
       "      <td>2025</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-04-30</th>\n",
       "      <td>3279.031</td>\n",
       "      <td>3335.746</td>\n",
       "      <td>-0.017002</td>\n",
       "      <td>-0.017148</td>\n",
       "      <td>2025</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-05-31</th>\n",
       "      <td>3347.487</td>\n",
       "      <td>3279.031</td>\n",
       "      <td>0.020877</td>\n",
       "      <td>0.020662</td>\n",
       "      <td>2025</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "               Close  Preclose  Raw_return  Log_return  Year  Month\n",
       "Day                                                                \n",
       "2025-01-31  3250.601       NaN         NaN         NaN  2025      1\n",
       "2025-02-28  3320.897  3250.601    0.021626    0.021395  2025      2\n",
       "2025-03-31  3335.746  3320.897    0.004471    0.004461  2025      3\n",
       "2025-04-30  3279.031  3335.746   -0.017002   -0.017148  2025      4\n",
       "2025-05-31  3347.487  3279.031    0.020877    0.020662  2025      5"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 方法2：使用resample取月末价格计算月度收益率\n",
    "# 这种方法直接使用月末价格计算收益率，更符合金融实践\n",
    "Month_data2 = data_new.resample('ME')['Close'].last().to_frame()\n",
    "Month_data2['Preclose'] = Month_data2['Close'].shift(1)\n",
    "Month_data2['Raw_return'] = Month_data2['Close'] / Month_data2['Preclose'] - 1\n",
    "Month_data2['Log_return'] = np.log(Month_data2['Close']) - np.log(Month_data2['Preclose'])\n",
    "\n",
    "# 添加年月信息\n",
    "Month_data2['Year'] = Month_data2.index.year\n",
    "Month_data2['Month'] = Month_data2.index.month\n",
    "\n",
    "# 显示结果\n",
    "Month_data2.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "8661290b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Preclose</th>\n",
       "      <th>Open</th>\n",
       "      <th>Highest</th>\n",
       "      <th>Lowest</th>\n",
       "      <th>Close</th>\n",
       "      <th>Raw_return</th>\n",
       "      <th>Log_return</th>\n",
       "      <th>Pct_change_return</th>\n",
       "      <th>Apply_return</th>\n",
       "      <th>Diff_div_return</th>\n",
       "      <th>Loop_return</th>\n",
       "      <th>Numpy_return</th>\n",
       "      <th>year</th>\n",
       "      <th>month</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Day</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2025-01-02</th>\n",
       "      <td>3351.763</td>\n",
       "      <td>3347.939</td>\n",
       "      <td>3351.722</td>\n",
       "      <td>3242.087</td>\n",
       "      <td>3262.561</td>\n",
       "      <td>-0.026613</td>\n",
       "      <td>-0.026974</td>\n",
       "      <td>NaN</td>\n",
       "      <td>-0.026613</td>\n",
       "      <td>NaN</td>\n",
       "      <td>-0.026613</td>\n",
       "      <td>-0.026613</td>\n",
       "      <td>2025</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-01-03</th>\n",
       "      <td>3262.561</td>\n",
       "      <td>3267.077</td>\n",
       "      <td>3273.566</td>\n",
       "      <td>3205.776</td>\n",
       "      <td>3211.430</td>\n",
       "      <td>-0.015672</td>\n",
       "      <td>-0.015796</td>\n",
       "      <td>-0.015672</td>\n",
       "      <td>-0.015672</td>\n",
       "      <td>-0.015672</td>\n",
       "      <td>-0.015672</td>\n",
       "      <td>-0.015672</td>\n",
       "      <td>2025</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-01-06</th>\n",
       "      <td>3211.430</td>\n",
       "      <td>3209.783</td>\n",
       "      <td>3219.488</td>\n",
       "      <td>3185.463</td>\n",
       "      <td>3206.923</td>\n",
       "      <td>-0.001403</td>\n",
       "      <td>-0.001404</td>\n",
       "      <td>-0.001403</td>\n",
       "      <td>-0.001403</td>\n",
       "      <td>-0.001403</td>\n",
       "      <td>-0.001403</td>\n",
       "      <td>-0.001403</td>\n",
       "      <td>2025</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-01-07</th>\n",
       "      <td>3206.923</td>\n",
       "      <td>3203.307</td>\n",
       "      <td>3230.853</td>\n",
       "      <td>3190.461</td>\n",
       "      <td>3229.644</td>\n",
       "      <td>0.007085</td>\n",
       "      <td>0.007060</td>\n",
       "      <td>0.007085</td>\n",
       "      <td>0.007085</td>\n",
       "      <td>0.007085</td>\n",
       "      <td>0.007085</td>\n",
       "      <td>0.007085</td>\n",
       "      <td>2025</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-01-08</th>\n",
       "      <td>3229.644</td>\n",
       "      <td>3218.858</td>\n",
       "      <td>3246.291</td>\n",
       "      <td>3175.725</td>\n",
       "      <td>3230.168</td>\n",
       "      <td>0.000162</td>\n",
       "      <td>0.000162</td>\n",
       "      <td>0.000162</td>\n",
       "      <td>0.000162</td>\n",
       "      <td>0.000162</td>\n",
       "      <td>0.000162</td>\n",
       "      <td>0.000162</td>\n",
       "      <td>2025</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-08-25</th>\n",
       "      <td>3825.759</td>\n",
       "      <td>3848.163</td>\n",
       "      <td>3883.562</td>\n",
       "      <td>3839.972</td>\n",
       "      <td>3883.562</td>\n",
       "      <td>0.015109</td>\n",
       "      <td>0.014996</td>\n",
       "      <td>0.015109</td>\n",
       "      <td>0.015109</td>\n",
       "      <td>0.015109</td>\n",
       "      <td>0.015109</td>\n",
       "      <td>0.015109</td>\n",
       "      <td>2025</td>\n",
       "      <td>8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-08-26</th>\n",
       "      <td>3883.562</td>\n",
       "      <td>3871.471</td>\n",
       "      <td>3888.599</td>\n",
       "      <td>3859.758</td>\n",
       "      <td>3868.382</td>\n",
       "      <td>-0.003909</td>\n",
       "      <td>-0.003916</td>\n",
       "      <td>-0.003909</td>\n",
       "      <td>-0.003909</td>\n",
       "      <td>-0.003909</td>\n",
       "      <td>-0.003909</td>\n",
       "      <td>-0.003909</td>\n",
       "      <td>2025</td>\n",
       "      <td>8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-08-27</th>\n",
       "      <td>3868.382</td>\n",
       "      <td>3869.612</td>\n",
       "      <td>3887.198</td>\n",
       "      <td>3800.350</td>\n",
       "      <td>3800.350</td>\n",
       "      <td>-0.017587</td>\n",
       "      <td>-0.017743</td>\n",
       "      <td>-0.017587</td>\n",
       "      <td>-0.017587</td>\n",
       "      <td>-0.017587</td>\n",
       "      <td>-0.017587</td>\n",
       "      <td>-0.017587</td>\n",
       "      <td>2025</td>\n",
       "      <td>8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-08-28</th>\n",
       "      <td>3800.350</td>\n",
       "      <td>3796.711</td>\n",
       "      <td>3845.087</td>\n",
       "      <td>3761.422</td>\n",
       "      <td>3843.597</td>\n",
       "      <td>0.011380</td>\n",
       "      <td>0.011315</td>\n",
       "      <td>0.011380</td>\n",
       "      <td>0.011380</td>\n",
       "      <td>0.011380</td>\n",
       "      <td>0.011380</td>\n",
       "      <td>0.011380</td>\n",
       "      <td>2025</td>\n",
       "      <td>8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-08-29</th>\n",
       "      <td>3843.597</td>\n",
       "      <td>3842.823</td>\n",
       "      <td>3867.606</td>\n",
       "      <td>3839.206</td>\n",
       "      <td>3857.927</td>\n",
       "      <td>0.003728</td>\n",
       "      <td>0.003721</td>\n",
       "      <td>0.003728</td>\n",
       "      <td>0.003728</td>\n",
       "      <td>0.003728</td>\n",
       "      <td>0.003728</td>\n",
       "      <td>0.003728</td>\n",
       "      <td>2025</td>\n",
       "      <td>8</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>161 rows × 14 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "            Preclose      Open   Highest    Lowest     Close  Raw_return  \\\n",
       "Day                                                                        \n",
       "2025-01-02  3351.763  3347.939  3351.722  3242.087  3262.561   -0.026613   \n",
       "2025-01-03  3262.561  3267.077  3273.566  3205.776  3211.430   -0.015672   \n",
       "2025-01-06  3211.430  3209.783  3219.488  3185.463  3206.923   -0.001403   \n",
       "2025-01-07  3206.923  3203.307  3230.853  3190.461  3229.644    0.007085   \n",
       "2025-01-08  3229.644  3218.858  3246.291  3175.725  3230.168    0.000162   \n",
       "...              ...       ...       ...       ...       ...         ...   \n",
       "2025-08-25  3825.759  3848.163  3883.562  3839.972  3883.562    0.015109   \n",
       "2025-08-26  3883.562  3871.471  3888.599  3859.758  3868.382   -0.003909   \n",
       "2025-08-27  3868.382  3869.612  3887.198  3800.350  3800.350   -0.017587   \n",
       "2025-08-28  3800.350  3796.711  3845.087  3761.422  3843.597    0.011380   \n",
       "2025-08-29  3843.597  3842.823  3867.606  3839.206  3857.927    0.003728   \n",
       "\n",
       "            Log_return  Pct_change_return  Apply_return  Diff_div_return  \\\n",
       "Day                                                                        \n",
       "2025-01-02   -0.026974                NaN     -0.026613              NaN   \n",
       "2025-01-03   -0.015796          -0.015672     -0.015672        -0.015672   \n",
       "2025-01-06   -0.001404          -0.001403     -0.001403        -0.001403   \n",
       "2025-01-07    0.007060           0.007085      0.007085         0.007085   \n",
       "2025-01-08    0.000162           0.000162      0.000162         0.000162   \n",
       "...                ...                ...           ...              ...   \n",
       "2025-08-25    0.014996           0.015109      0.015109         0.015109   \n",
       "2025-08-26   -0.003916          -0.003909     -0.003909        -0.003909   \n",
       "2025-08-27   -0.017743          -0.017587     -0.017587        -0.017587   \n",
       "2025-08-28    0.011315           0.011380      0.011380         0.011380   \n",
       "2025-08-29    0.003721           0.003728      0.003728         0.003728   \n",
       "\n",
       "            Loop_return  Numpy_return  year  month  \n",
       "Day                                                 \n",
       "2025-01-02    -0.026613     -0.026613  2025      1  \n",
       "2025-01-03    -0.015672     -0.015672  2025      1  \n",
       "2025-01-06    -0.001403     -0.001403  2025      1  \n",
       "2025-01-07     0.007085      0.007085  2025      1  \n",
       "2025-01-08     0.000162      0.000162  2025      1  \n",
       "...                 ...           ...   ...    ...  \n",
       "2025-08-25     0.015109      0.015109  2025      8  \n",
       "2025-08-26    -0.003909     -0.003909  2025      8  \n",
       "2025-08-27    -0.017587     -0.017587  2025      8  \n",
       "2025-08-28     0.011380      0.011380  2025      8  \n",
       "2025-08-29     0.003728      0.003728  2025      8  \n",
       "\n",
       "[161 rows x 14 columns]"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# “1990-12-12”日期格式 里面的year年份 month月份 day 直接提出取来\n",
    "data_new2 = data_new.copy()\n",
    "data_new2['year'] = data_new2.index.year\n",
    "data_new2['month'] = data_new2.index.month\n",
    "data_new2\n",
    "# 使用的时间、日期格式提取 字符串提出的方式 前四个字符当作年份 6-7字符是月份 提取出来的是字符串 变成数值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "dcf614d7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>Log_return</th>\n",
       "      <th>Raw_Return</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>year</th>\n",
       "      <th>month</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"8\" valign=\"top\">2025</th>\n",
       "      <th>1</th>\n",
       "      <td>-0.030647</td>\n",
       "      <td>-0.030182</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0.021395</td>\n",
       "      <td>0.021626</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0.004461</td>\n",
       "      <td>0.004471</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>-0.017148</td>\n",
       "      <td>-0.017002</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>0.020662</td>\n",
       "      <td>0.020877</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>0.028547</td>\n",
       "      <td>0.028959</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>0.036707</td>\n",
       "      <td>0.037389</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>0.076666</td>\n",
       "      <td>0.079682</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "            Log_return  Raw_Return\n",
       "year month                        \n",
       "2025 1       -0.030647   -0.030182\n",
       "     2        0.021395    0.021626\n",
       "     3        0.004461    0.004471\n",
       "     4       -0.017148   -0.017002\n",
       "     5        0.020662    0.020877\n",
       "     6        0.028547    0.028959\n",
       "     7        0.036707    0.037389\n",
       "     8        0.076666    0.079682"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 方法3：使用groupby函数按年月分组计算月度收益率\n",
    "# 首先提取年月信息\n",
    "data_new3 = data_new.copy()\n",
    "data_new3['year'] = data_new3.index.year\n",
    "data_new3['month'] = data_new3.index.month\n",
    "\n",
    "# 使用groupby按年月分组，然后对每组的对数收益率求和\n",
    "Month_data3 = data_new3.groupby(['year', 'month'])['Log_return'].sum().to_frame()\n",
    "Month_data3['Raw_Return'] = np.exp(Month_data3['Log_return']) - 1\n",
    "\n",
    "# 显示结果\n",
    "Month_data3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "bfb33ddd",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "方法4结果:\n",
      "            Log_return  Raw_Return\n",
      "year month                        \n",
      "2025 1       -0.030647   -0.030182\n",
      "     2        0.021395    0.021626\n",
      "     3        0.004461    0.004471\n",
      "     4       -0.017148   -0.017002\n",
      "     5        0.020662    0.020877\n",
      "\n",
      "方法5结果 (包含多个统计量):\n",
      "           Log_return                           Raw_return          \n",
      "                  sum      mean       std count       mean       std\n",
      "year month                                                          \n",
      "2025 1      -0.030647 -0.001703  0.010875    18  -0.001645  0.010858\n",
      "     2       0.021395  0.001189  0.008500    18   0.001223  0.008483\n",
      "     3       0.004461  0.000212  0.006369    21   0.000232  0.006387\n",
      "     4      -0.017148 -0.000817  0.018027    21  -0.000665  0.017447\n",
      "     5       0.020662  0.001087  0.005664    19   0.001103  0.005671\n"
     ]
    }
   ],
   "source": [
    "# 方法4：使用apply和lambda函数进行更灵活的分组计算\n",
    "# 这种方法可以对每个月的数据进行更复杂的操作\n",
    "Month_data4 = pd.DataFrame(\n",
    "    data_new3.groupby(['year', 'month'])['Log_return'].apply(lambda x: sum(x)))\n",
    "Month_data4.columns = ['Log_return']\n",
    "Month_data4['Raw_Return'] = np.exp(Month_data4['Log_return']) - 1\n",
    "\n",
    "# 方法5：使用agg函数同时计算多个统计量\n",
    "Month_data5 = data_new3.groupby(['year', 'month']).agg({\n",
    "    'Log_return': ['sum', 'mean', 'std', 'count'],\n",
    "    'Raw_return': ['mean', 'std']\n",
    "})\n",
    "\n",
    "# 显示结果\n",
    "print(\"方法4结果:\")\n",
    "print(Month_data4.head())\n",
    "print(\"\\n方法5结果 (包含多个统计量):\")\n",
    "print(Month_data5.head())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "c5a595e8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "季度对数收益率汇总:\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Log_return</th>\n",
       "      <th>Raw_Return</th>\n",
       "      <th>Year</th>\n",
       "      <th>Quarter</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Day</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2025-03-31</th>\n",
       "      <td>-0.004790</td>\n",
       "      <td>-0.004779</td>\n",
       "      <td>2025</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-06-30</th>\n",
       "      <td>0.032061</td>\n",
       "      <td>0.032580</td>\n",
       "      <td>2025</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-09-30</th>\n",
       "      <td>0.113373</td>\n",
       "      <td>0.120049</td>\n",
       "      <td>2025</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "            Log_return  Raw_Return  Year  Quarter\n",
       "Day                                              \n",
       "2025-03-31   -0.004790   -0.004779  2025        1\n",
       "2025-06-30    0.032061    0.032580  2025        2\n",
       "2025-09-30    0.113373    0.120049  2025        3"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "季度末价格计算的收益率:\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Close</th>\n",
       "      <th>Preclose</th>\n",
       "      <th>Raw_return</th>\n",
       "      <th>Log_return</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Day</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2025-03-31</th>\n",
       "      <td>3335.746</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-06-30</th>\n",
       "      <td>3444.426</td>\n",
       "      <td>3335.746</td>\n",
       "      <td>0.032580</td>\n",
       "      <td>0.032061</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-09-30</th>\n",
       "      <td>3857.927</td>\n",
       "      <td>3444.426</td>\n",
       "      <td>0.120049</td>\n",
       "      <td>0.113373</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "               Close  Preclose  Raw_return  Log_return\n",
       "Day                                                   \n",
       "2025-03-31  3335.746       NaN         NaN         NaN\n",
       "2025-06-30  3444.426  3335.746    0.032580    0.032061\n",
       "2025-09-30  3857.927  3444.426    0.120049    0.113373"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 计算季度收益率\n",
    "# 方法1：使用resample函数的'QE'参数（季度末）\n",
    "Quarter_data1 = data_new.resample('QE')['Log_return'].sum().to_frame(name='Log_return')\n",
    "Quarter_data1['Raw_Return'] = np.exp(Quarter_data1['Log_return']) - 1\n",
    "Quarter_data1['Year'] = Quarter_data1.index.year\n",
    "Quarter_data1['Quarter'] = Quarter_data1.index.quarter\n",
    "\n",
    "# 方法2：使用季度末价格计算\n",
    "Quarter_data2 = data_new.resample('QE')['Close'].last().to_frame()\n",
    "Quarter_data2['Preclose'] = Quarter_data2['Close'].shift(1)\n",
    "Quarter_data2['Raw_return'] = Quarter_data2['Close'] / Quarter_data2['Preclose'] - 1\n",
    "Quarter_data2['Log_return'] = np.log(Quarter_data2['Close']) - np.log(Quarter_data2['Preclose'])\n",
    "\n",
    "# 显示结果\n",
    "print(\"季度对数收益率汇总:\")\n",
    "Quarter_data1\n",
    "print(\"\\n季度末价格计算的收益率:\")\n",
    "Quarter_data2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "c285620d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "年度对数收益率汇总:\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Log_return</th>\n",
       "      <th>Raw_Return</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Day</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2025-12-31</th>\n",
       "      <td>0.140644</td>\n",
       "      <td>0.151014</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "            Log_return  Raw_Return\n",
       "Day                               \n",
       "2025-12-31    0.140644    0.151014"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "年末价格计算的收益率:\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Close</th>\n",
       "      <th>Preclose</th>\n",
       "      <th>Raw_return</th>\n",
       "      <th>Log_return</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Day</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2025-12-31</th>\n",
       "      <td>3857.927</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "               Close  Preclose  Raw_return  Log_return\n",
       "Day                                                   \n",
       "2025-12-31  3857.927       NaN         NaN         NaN"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "使用groupby计算的年度收益率:\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Log_return</th>\n",
       "      <th>Raw_Return</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>year</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2025</th>\n",
       "      <td>0.140644</td>\n",
       "      <td>0.151014</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "      Log_return  Raw_Return\n",
       "year                        \n",
       "2025    0.140644    0.151014"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 计算年度收益率\n",
    "# 方法1：使用resample函数的'YE'参数（年末）\n",
    "Year_data1 = data_new.resample('YE')['Log_return'].sum().to_frame(name='Log_return')\n",
    "Year_data1['Raw_Return'] = np.exp(Year_data1['Log_return']) - 1\n",
    "\n",
    "# 方法2：使用年末价格计算\n",
    "Year_data2 = data_new.resample('YE')['Close'].last().to_frame()\n",
    "Year_data2['Preclose'] = Year_data2['Close'].shift(1)\n",
    "Year_data2['Raw_return'] = Year_data2['Close'] / Year_data2['Preclose'] - 1\n",
    "Year_data2['Log_return'] = np.log(Year_data2['Close']) - np.log(Year_data2['Preclose'])\n",
    "\n",
    "# 方法3：使用groupby按年分组\n",
    "data_new4 = data_new.copy()\n",
    "data_new4['year'] = data_new4.index.year\n",
    "Year_data3 = data_new4.groupby('year')['Log_return'].sum().to_frame()\n",
    "Year_data3['Raw_Return'] = np.exp(Year_data3['Log_return']) - 1\n",
    "\n",
    "# 显示结果\n",
    "print(\"年度对数收益率汇总:\")\n",
    "Year_data1\n",
    "print(\"\\n年末价格计算的收益率:\")\n",
    "Year_data2\n",
    "print(\"\\n使用groupby计算的年度收益率:\")\n",
    "Year_data3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "fefa35c3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "滚动收益率 (基于对数收益率累加):\n",
      "            Rolling_5d_Return  Rolling_10d_Return  Rolling_20d_Return  \\\n",
      "Day                                                                     \n",
      "2025-08-25           0.041720            0.064705            0.079386   \n",
      "2025-08-26           0.037854            0.055229            0.071660   \n",
      "2025-08-27           0.009065            0.031732            0.051064   \n",
      "2025-08-28           0.019225            0.048318            0.075671   \n",
      "2025-08-29           0.008408            0.043594            0.083702   \n",
      "\n",
      "            Rolling_30d_Return  Rolling_60d_Return  \n",
      "Day                                                 \n",
      "2025-08-25            0.103394            0.160143  \n",
      "2025-08-26            0.103676            0.150627  \n",
      "2025-08-27            0.084644            0.125628  \n",
      "2025-08-28            0.092916            0.135781  \n",
      "2025-08-29            0.091511            0.139592  \n",
      "\n",
      "滚动收益率 (基于价格变化):\n",
      "            Rolling_5d_Price_Return  Rolling_10d_Price_Return  \\\n",
      "Day                                                             \n",
      "2025-08-25                 0.041720                  0.064705   \n",
      "2025-08-26                 0.037854                  0.055229   \n",
      "2025-08-27                 0.009065                  0.031732   \n",
      "2025-08-28                 0.019225                  0.048318   \n",
      "2025-08-29                 0.008408                  0.043594   \n",
      "\n",
      "            Rolling_20d_Price_Return  Rolling_30d_Price_Return  \\\n",
      "Day                                                              \n",
      "2025-08-25                  0.079386                  0.103394   \n",
      "2025-08-26                  0.071660                  0.103676   \n",
      "2025-08-27                  0.051064                  0.084644   \n",
      "2025-08-28                  0.075671                  0.092916   \n",
      "2025-08-29                  0.083702                  0.091511   \n",
      "\n",
      "            Rolling_60d_Price_Return  \n",
      "Day                                   \n",
      "2025-08-25                  0.160143  \n",
      "2025-08-26                  0.150627  \n",
      "2025-08-27                  0.125628  \n",
      "2025-08-28                  0.135781  \n",
      "2025-08-29                  0.139592  \n"
     ]
    }
   ],
   "source": [
    "# 计算滚动收益率（例如：过去30天、60天、90天的收益率 注意这里指的是前30个观测值）\n",
    "# 这在金融分析中非常常见，用于观察不同时间窗口的收益表现\n",
    "\n",
    "# 方法1：使用rolling窗口函数计算滚动对数收益率之和\n",
    "rolling_returns = pd.DataFrame()\n",
    "for window in [5, 10, 20, 30, 60]:\n",
    "    # 计算滚动窗口的对数收益率之和\n",
    "    rolling_log_return = data_new['Log_return'].rolling(window=window).sum()\n",
    "    # 转换为原始收益率\n",
    "    rolling_returns[f'Rolling_{window}d_Return'] = np.exp(rolling_log_return) - 1\n",
    "\n",
    "# 方法2：使用pct_change计算滚动价格变化\n",
    "rolling_price_returns = pd.DataFrame()\n",
    "for window in [5, 10, 20, 30, 60]:\n",
    "    rolling_price_returns[f'Rolling_{window}d_Price_Return'] = data_new['Close'].pct_change(periods=window)\n",
    "\n",
    "# 显示结果\n",
    "print(\"滚动收益率 (基于对数收益率累加):\")\n",
    "print(rolling_returns.tail())\n",
    "print(\"\\n滚动收益率 (基于价格变化):\")\n",
    "print(rolling_price_returns.tail())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "558db7a4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "不同方法计算的累积收益率:\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Cumulative_Log_Return</th>\n",
       "      <th>Cumulative_Return</th>\n",
       "      <th>Cumulative_Return_Prod</th>\n",
       "      <th>Cumulative_Return_Alt</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Day</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2025-01-02</th>\n",
       "      <td>-0.026974</td>\n",
       "      <td>-0.026613</td>\n",
       "      <td>-0.026613</td>\n",
       "      <td>-0.026613</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-01-03</th>\n",
       "      <td>-0.042770</td>\n",
       "      <td>-0.041868</td>\n",
       "      <td>-0.041868</td>\n",
       "      <td>-0.041868</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-01-06</th>\n",
       "      <td>-0.044175</td>\n",
       "      <td>-0.043213</td>\n",
       "      <td>-0.043213</td>\n",
       "      <td>-0.043213</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-01-07</th>\n",
       "      <td>-0.037115</td>\n",
       "      <td>-0.036434</td>\n",
       "      <td>-0.036434</td>\n",
       "      <td>-0.036434</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-01-08</th>\n",
       "      <td>-0.036952</td>\n",
       "      <td>-0.036278</td>\n",
       "      <td>-0.036278</td>\n",
       "      <td>-0.036278</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-08-25</th>\n",
       "      <td>0.147266</td>\n",
       "      <td>0.158662</td>\n",
       "      <td>0.158662</td>\n",
       "      <td>0.158662</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-08-26</th>\n",
       "      <td>0.143350</td>\n",
       "      <td>0.154134</td>\n",
       "      <td>0.154134</td>\n",
       "      <td>0.154134</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-08-27</th>\n",
       "      <td>0.125607</td>\n",
       "      <td>0.133836</td>\n",
       "      <td>0.133836</td>\n",
       "      <td>0.133836</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-08-28</th>\n",
       "      <td>0.136922</td>\n",
       "      <td>0.146739</td>\n",
       "      <td>0.146739</td>\n",
       "      <td>0.146739</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-08-29</th>\n",
       "      <td>0.140644</td>\n",
       "      <td>0.151014</td>\n",
       "      <td>0.151014</td>\n",
       "      <td>0.151014</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>161 rows × 4 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "            Cumulative_Log_Return  Cumulative_Return  Cumulative_Return_Prod  \\\n",
       "Day                                                                            \n",
       "2025-01-02              -0.026974          -0.026613               -0.026613   \n",
       "2025-01-03              -0.042770          -0.041868               -0.041868   \n",
       "2025-01-06              -0.044175          -0.043213               -0.043213   \n",
       "2025-01-07              -0.037115          -0.036434               -0.036434   \n",
       "2025-01-08              -0.036952          -0.036278               -0.036278   \n",
       "...                           ...                ...                     ...   \n",
       "2025-08-25               0.147266           0.158662                0.158662   \n",
       "2025-08-26               0.143350           0.154134                0.154134   \n",
       "2025-08-27               0.125607           0.133836                0.133836   \n",
       "2025-08-28               0.136922           0.146739                0.146739   \n",
       "2025-08-29               0.140644           0.151014                0.151014   \n",
       "\n",
       "            Cumulative_Return_Alt  \n",
       "Day                                \n",
       "2025-01-02              -0.026613  \n",
       "2025-01-03              -0.041868  \n",
       "2025-01-06              -0.043213  \n",
       "2025-01-07              -0.036434  \n",
       "2025-01-08              -0.036278  \n",
       "...                           ...  \n",
       "2025-08-25               0.158662  \n",
       "2025-08-26               0.154134  \n",
       "2025-08-27               0.133836  \n",
       "2025-08-28               0.146739  \n",
       "2025-08-29               0.151014  \n",
       "\n",
       "[161 rows x 4 columns]"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 计算累积收益率\n",
    "# 累积收益率用于观察长期投资表现，从某个起始点开始累积\n",
    "\n",
    "# 方法1：使用对数收益率累加后转换\n",
    "# 这是最准确的方法，特别是对于长期累积\n",
    "cumulative_returns = pd.DataFrame()\n",
    "cumulative_returns['Cumulative_Log_Return'] = data_new['Log_return'].cumsum()\n",
    "cumulative_returns['Cumulative_Return'] = np.exp(cumulative_returns['Cumulative_Log_Return']) - 1\n",
    "\n",
    "# 方法2：使用cumprod函数直接累乘(1+r)\n",
    "# 这种方法在金融实践中也很常见\n",
    "cumulative_returns['Cumulative_Return_Prod'] = (1 + data_new['Raw_return']).cumprod() - 1\n",
    "\n",
    "# 方法3：使用pandas的累积函数\n",
    "cumulative_returns['Cumulative_Return_Alt'] = data_new['Raw_return'].add(1).cumprod().sub(1)\n",
    "\n",
    "# 显示结果\n",
    "print(\"不同方法计算的累积收益率:\")\n",
    "cumulative_returns"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
